home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / gdb / gdb_18s.zoo / st-infru.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-25  |  33.2 KB  |  1,168 lines

  1. /* this one hacked up for st by jrd */
  2.  
  3. /* Start and stop the inferior process, for GDB.
  4.    Copyright (C) 1986, 1987, 1988 Free Software Foundation, Inc.
  5.  
  6. GDB is distributed in the hope that it will be useful, but WITHOUT ANY
  7. WARRANTY.  No author or distributor accepts responsibility to anyone
  8. for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.
  10. Refer to the GDB General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute GDB,
  13. but only under the conditions described in the GDB General Public
  14. License.  A copy of this license is supposed to have been given to you
  15. along with GDB so you can know your rights and responsibilities.  It
  16. should be in a file named COPYING.  Among other things, the copyright
  17. notice and this notice must be preserved on all copies.
  18.  
  19. In other words, go ahead and share GDB, but don't try to stop
  20. anyone else from sharing it farther.  Help stamp out software hoarding!
  21. */
  22.  
  23. #include "defs.h"
  24. #include "param.h"
  25. #include "symtab.h"
  26. #include "frame.h"
  27. #include "inferior.h"
  28. #include "wait.h"
  29.  
  30. #include <stdio.h>
  31. #include <signal.h>
  32.  
  33. #include <st-out.h>
  34.  
  35. extern char *sys_siglist[];
  36. extern int errno;
  37.  
  38. /* Tables of how to react to signals; the user sets them.  */
  39.  
  40. static char signal_stop[NSIG];
  41. static char signal_print[NSIG];
  42. static char signal_program[NSIG];
  43.  
  44. /* Nonzero if breakpoints are now inserted in the inferior.  */
  45.  
  46. static int breakpoints_inserted;
  47.  
  48. /* Function inferior was in as of last step command.  */
  49.  
  50. static struct symbol *step_start_function;
  51.  
  52. /* This is the sequence of bytes we insert for a breakpoint.  */
  53.  
  54. static char break_insn[] = BREAKPOINT;
  55.  
  56. /* Nonzero => address for special breakpoint for resuming stepping.  */ 
  57.  
  58. static CORE_ADDR step_resume_break_address;
  59.  
  60. /* Original contents of the byte where the special breakpoint is.  */
  61.  
  62. static char step_resume_break_shadow[sizeof break_insn];
  63.  
  64. /* Nonzero means the special breakpoint is a duplicate
  65.    so it has not itself been inserted.  */
  66.  
  67. static int step_resume_break_duplicate;
  68.  
  69. /* Nonzero if we are expecting a trace trap and should proceed from it.
  70.    2 means expecting 2 trace traps and should continue both times.
  71.    That occurs when we tell sh to exec the program: we will get
  72.    a trap after the exec of sh and a second when the program is exec'd.  */
  73.  
  74. static int trap_expected;
  75.  
  76. /* Nonzero if the next time we try to continue the inferior, it will
  77.    step one instruction and generate a spurious trace trap.
  78.    This is used to compensate for a bug in HP-UX.  */
  79.  
  80. static int trap_expected_after_continue;
  81.  
  82. /* Nonzero means expecting a trace trap
  83.    and should stop the inferior and return silently when it happens.  */
  84.  
  85. static int stop_after_trap;
  86.  
  87. /* Nonzero means expecting a trace trap due to attaching to a process.  */
  88.  
  89. static int stop_after_attach;
  90.  
  91. /* Nonzero if pc has been changed by the debugger
  92.    since the inferior stopped.  */
  93.  
  94. int pc_changed;
  95.  
  96. /* Nonzero if proceed is being used for a "finish" command or a similar
  97.    situation when stop_registers should be saved.  */
  98.  
  99. int proceed_to_finish;
  100.  
  101. /* Nonzero if debugging a remote machine via a serial link or ethernet.  */
  102.  
  103. int remote_debugging;
  104.  
  105. /* Save register contents here when about to pop a stack dummy frame.  */
  106.  
  107. char stop_registers[REGISTER_BYTES];
  108.  
  109. /* Nonzero if program stopped due to error trying to insert breakpoints.  */
  110.  
  111. static int breakpoints_failed;
  112.  
  113. /* Nonzero if inferior is in sh before our program got exec'd.  */
  114.  
  115. static int running_in_shell;
  116.  
  117. /* Nonzero after stop if current stack frame should be printed.  */
  118.  
  119. static int stop_print_frame;
  120.  
  121. static void insert_step_breakpoint ();
  122. static void remove_step_breakpoint ();
  123. static void wait_for_inferior ();
  124. static void normal_stop ();
  125.  
  126.  
  127. /* Clear out all variables saying what to do when inferior is continued.
  128.    First do this, then set the ones you want, then call `proceed'.  */
  129.  
  130. void
  131. clear_proceed_status ()
  132. {
  133.   trap_expected = 0;
  134.   step_range_start = 0;
  135.   step_range_end = 0;
  136.   step_frame = 0;
  137.   step_over_calls = -1;
  138.   step_resume_break_address = 0;
  139.   stop_after_trap = 0;
  140.   stop_after_attach = 0;
  141.   proceed_to_finish = 0;
  142.  
  143.   /* Discard any remaining commands left by breakpoint we had stopped at.  */
  144.   clear_breakpoint_commands ();
  145. }
  146.  
  147. /* Basic routine for continuing the program in various fashions.
  148.  
  149.    ADDR is the address to resume at, or -1 for resume where stopped.
  150.    SIGNAL is the signal to give it, or 0 for none,
  151.      or -1 for act according to how it stopped.
  152.    STEP is nonzero if should trap after one instruction.
  153.      -1 means return after that and print nothing.
  154.      You should probably set various step_... variables
  155.      before calling here, if you are stepping.
  156.  
  157.    You should call clear_proceed_status before calling proceed.  */
  158.  
  159. void
  160. proceed (addr, signal, step)
  161.      CORE_ADDR addr;
  162.      int signal;
  163.      int step;
  164. {
  165.   int oneproc = 0;
  166.  
  167.   if (step > 0)
  168.     step_start_function = find_pc_function (read_pc ());
  169.   if (step < 0)
  170.     stop_after_trap = 1;
  171.  
  172.   if (addr == -1)
  173.     {
  174.       /* If there is a breakpoint at the address we will resume at,
  175.      step one instruction before inserting breakpoints
  176.      so that we do not stop right away.  */
  177.  
  178.       if (!pc_changed && breakpoint_here_p (read_pc ()))
  179.     oneproc = 1;
  180.     }
  181.   else
  182.     write_register (PC_REGNUM, addr);
  183.  
  184.   if (trap_expected_after_continue)
  185.     {
  186.       /* If (step == 0), a trap will be automatically generated after
  187.      the first instruction is executed.  Force step one
  188.      instruction to clear this condition.  This should not occur
  189.      if step is nonzero, but it is harmless in that case.  */
  190.       oneproc = 1;
  191.       trap_expected_after_continue = 0;
  192.     }
  193.  
  194.   if (oneproc)
  195.     /* We will get a trace trap after one instruction.
  196.        Continue it automatically and insert breakpoints then.  */
  197.     trap_expected = 1;
  198.   else
  199.     {
  200.       int temp = insert_breakpoints ();
  201.       if (temp)
  202.     {
  203.       print_sys_errmsg ("ptrace", temp);
  204.       error ("Cannot insert breakpoints.\n\
  205. The same program may be running in another process.");
  206.     }
  207.       breakpoints_inserted = 1;
  208.     }
  209.  
  210.   /* Install inferior's terminal modes.  */
  211.   terminal_inferior ();
  212.  
  213.   if (signal >= 0)
  214.     stop_signal = signal;
  215.   /* If this signal should not be seen by program,
  216.      give it zero.  Used for debugging signals.  */
  217.   else if (stop_signal < NSIG && !signal_program[stop_signal])
  218.     stop_signal= 0;
  219.  
  220.   /* Resume inferior.  */
  221.   resume (oneproc || step, stop_signal);
  222.  
  223.   /* Wait for it to stop (if not standalone)
  224.      and in any case decode why it stopped, and act accordingly.  */
  225.  
  226.   wait_for_inferior ();
  227.   normal_stop ();
  228. }
  229.  
  230. /* Writing the inferior pc as a register calls this function
  231.    to inform infrun that the pc has been set in the debugger.  */
  232.  
  233. writing_pc (val)
  234.      CORE_ADDR val;
  235. {
  236.   stop_pc = val;
  237.   pc_changed = 1;
  238. }
  239.  
  240. /* Start an inferior process for the first time.
  241.    Actually it was started by the fork that created it,
  242.    but it will have stopped one instruction after execing sh.
  243.    Here we must get it up to actual execution of the real program.  */
  244.  
  245. start_inferior ()
  246. {
  247.   /* We will get a trace trap after one instruction.
  248.      Continue it automatically.  Eventually (after shell does an exec)
  249.      it will get another trace trap.  Then insert breakpoints and continue.  */
  250.   trap_expected = 2;
  251.   running_in_shell = 0;        /* Set to 1 at first SIGTRAP, 0 at second.  */
  252.   trap_expected_after_continue = 0;
  253.   breakpoints_inserted = 0;
  254.   mark_breakpoints_out ();
  255.  
  256.   /* Set up the "saved terminal modes" of the inferior
  257.      based on what modes we are starting it with.  */
  258.   terminal_init_inferior ();
  259.  
  260.   /* Install inferior's terminal modes.  */
  261.   terminal_inferior ();
  262.  
  263.   if (remote_debugging)
  264.     {
  265.       trap_expected = 0;
  266.       fetch_inferior_registers();
  267.       set_current_frame (read_register(FP_REGNUM));
  268.       stop_frame = get_current_frame();
  269.       inferior_pid = 3;
  270.       if (insert_breakpoints())
  271.     fatal("Can't insert breakpoints");
  272.       breakpoints_inserted = 1;
  273.       proceed(-1, -1, 0);
  274.     }
  275.   else
  276.     {
  277.       wait_for_inferior ();
  278.       normal_stop ();
  279.     }
  280. }
  281.  
  282. /* Start remote-debugging of a machine over a serial link.  */
  283.  
  284. void
  285. start_remote ()
  286. {
  287.   clear_proceed_status ();
  288.   running_in_shell = 0;
  289.   trap_expected = 0;
  290.   inferior_pid = 3;
  291.   breakpoints_inserted = 0;
  292.   mark_breakpoints_out ();
  293.   wait_for_inferior ();
  294.   normal_stop();
  295. }
  296.  
  297. #ifdef ATTACH_DETACH
  298.  
  299. /* Attach to process PID, then initialize for debugging it
  300.    and wait for the trace-trap that results from attaching.  */
  301.  
  302. void
  303. attach_program (pid)
  304.      int pid;
  305. {
  306.   attach (pid);
  307.   inferior_pid = pid;
  308.  
  309.   mark_breakpoints_out ();
  310.   terminal_init_inferior ();
  311.   clear_proceed_status ();
  312.   stop_after_attach = 1;
  313.   /*proceed (-1, 0, -2);*/
  314.   wait_for_inferior ();
  315.   normal_stop ();
  316. }
  317. #endif /* ATTACH_DETACH */
  318.  
  319. /* Wait for control to return from inferior to debugger.
  320.    If inferior gets a signal, we may decide to start it up again
  321.    instead of returning.  That is why there is a loop in this function.
  322.    When this function actually returns it means the inferior
  323.    should be left stopped and GDB should read more commands.  */
  324.  
  325. static void
  326. wait_for_inferior ()
  327. {
  328.   register int pid;
  329.    WAITTYPE w;
  330.   CORE_ADDR pc;
  331.   int tem;
  332.   int another_trap;
  333.   int random_signal;
  334.   CORE_ADDR stop_sp;
  335.   int stop_step_resume_break;
  336.   int newmisc;
  337.   int newfun_pc;
  338.   struct symbol *newfun;
  339.   struct symtab_and_line sal;
  340.   int prev_pc;
  341.  
  342.   prev_pc = read_pc ();
  343.  
  344.   while (1)
  345.     {
  346. /*
  347.       if (remote_debugging)
  348.     remote_wait (&w);
  349.       else
  350. */
  351.     {
  352.       pid = st_wait_kludge (&w);
  353.       if (pid != inferior_pid)
  354.         continue;
  355.     }
  356.  
  357.       pc_changed = 0;
  358.       fetch_inferior_registers ();
  359.       stop_pc = read_pc ();
  360.       set_current_frame (read_register (FP_REGNUM));
  361.       stop_frame = get_current_frame ();
  362.       stop_sp = read_register (SP_REGNUM);
  363.       another_trap = 0;
  364.       stop_breakpoint = 0;
  365.       stop_step = 0;
  366.       stop_stack_dummy = 0;
  367.       stop_print_frame = 1;
  368.       stop_step_resume_break = 0;
  369.       random_signal = 0;
  370.       breakpoints_failed = 0;
  371.  
  372.       /* Look at the cause of the stop, and decide what to do.
  373.      The alternatives are:
  374.      1) break; to really stop and return to the debugger,
  375.      2) drop through to start up again
  376.        (set another_trap to 1 to single step once)
  377.      3) set random_signal to 1, and the decision between 1 and 2 
  378.        will be made according to the signal handling tables.  */
  379.  
  380.       if (WIFEXITED (w))
  381.     {
  382.       terminal_ours_for_output ();
  383.       if (WRETCODE (w))
  384.        printf_filtered ("\nProgram exited with code 0%o.\n", WRETCODE (w));
  385.       else
  386.        printf_filtered ("\nProgram exited normally.\n");
  387.       fflush (stdout);
  388.       inferior_died ();
  389.       stop_print_frame = 0;
  390.       break;
  391.     }
  392.       else if (!WIFSTOPPED (w))
  393.     {
  394.       stop_print_frame = 0;
  395.       stop_signal = WTERMSIG (w);
  396.       terminal_ours_for_output ();
  397.       kill_inferior ();
  398.      printf_filtered ("\nProgram terminated with signal %d, %s\n",
  399.           stop_signal,
  400.           stop_signal < NSIG
  401.           ? sys_siglist[stop_signal]
  402.           : "(undocumented)");
  403.      printf_filtered ("The inferior process no longer exists.\n");
  404.       fflush (stdout);
  405.       break;
  406.     }
  407.       else
  408.     {
  409.       stop_signal = WSTOPSIG (w);
  410.  
  411.       /* First, distinguish signals caused by the debugger from signals
  412.          that have to do with the program's own actions.
  413.          Note that breakpoint insns may cause SIGTRAP or SIGILL
  414.          or SIGEMT, depending on the operating system version.
  415.          Here we detect when a SIGILL or SIGEMT is really a breakpoint
  416.          and change it to SIGTRAP.  */
  417.  
  418.       if (stop_signal == SIGTRAP
  419.           || (breakpoints_inserted &&
  420.           (stop_signal == SIGILL
  421.            || stop_signal == SIGEMT))
  422.           || stop_after_attach)
  423.         {
  424.           if (stop_signal == SIGTRAP && stop_after_trap)
  425.         {
  426.           stop_print_frame = 0;
  427.           break;
  428.         }
  429.           if (stop_after_attach)
  430.         break;
  431.           /* Don't even think about breakpoints
  432.          if still running the shell that will exec the program
  433.          or if just proceeded over a breakpoint.  */
  434.           if (stop_signal == SIGTRAP && trap_expected
  435.           && (!step_resume_break_address))
  436.         stop_breakpoint = 0;
  437.           else
  438.         /* See if there is a breakpoint at the current PC.  */
  439. #if DECR_PC_AFTER_BREAK
  440.         /* Notice the case of stepping through a jump
  441.            that leads just after a breakpoint.
  442.            Don't confuse that with hitting the breakpoint.
  443.            What we check for is that 1) stepping is going on
  444.            and 2) the pc before the last insn does not match
  445.            the address of the breakpoint before the current pc.  */
  446.         if (!(prev_pc != stop_pc - DECR_PC_AFTER_BREAK
  447.               && step_range_end && !step_resume_break_address))
  448. #endif /* DECR_PC_AFTER_BREAK not zero */
  449.           {
  450.             select_frame (stop_frame, 0); /* For condition exprs. */
  451.             stop_breakpoint = breakpoint_stop_status (stop_pc, stop_frame);
  452.             /* Following in case break condition called a function.  */
  453.             stop_print_frame = 1;
  454.             if (stop_breakpoint && DECR_PC_AFTER_BREAK)
  455.               {
  456.             stop_pc -= DECR_PC_AFTER_BREAK;
  457.             write_register (PC_REGNUM, stop_pc);
  458.             pc_changed = 0;
  459.               }
  460.           }
  461.           /* See if we stopped at the special breakpoint for
  462.          stepping over a subroutine call.  */
  463.           if (stop_pc - DECR_PC_AFTER_BREAK == step_resume_break_address
  464.           && step_resume_break_address)
  465.         {
  466.           stop_step_resume_break = 1;
  467.           if (DECR_PC_AFTER_BREAK)
  468.             {
  469.               stop_pc -= DECR_PC_AFTER_BREAK;
  470.               write_register (PC_REGNUM, stop_pc);
  471.               pc_changed = 0;
  472.             }
  473.         }
  474.  
  475.           if (stop_signal == SIGTRAP)
  476.         random_signal
  477.           = !(stop_breakpoint || trap_expected
  478.               || stop_step_resume_break
  479.               || (stop_sp INNER_THAN stop_pc && stop_pc INNER_THAN stop_frame)
  480.               || (step_range_end && !step_resume_break_address));
  481.           else
  482.         {
  483.           random_signal
  484.             = !(stop_breakpoint || stop_step_resume_break
  485.             || (stop_sp INNER_THAN stop_pc
  486.                 && stop_pc INNER_THAN stop_frame));
  487.           if (!random_signal)
  488.             stop_signal = SIGTRAP;
  489.         }
  490.         }
  491.       else
  492.         random_signal = 1;
  493.  
  494.       /* For the program's own signals, act according to
  495.          the signal handling tables.  */
  496.  
  497.       if (random_signal
  498.           && !(running_in_shell && stop_signal == SIGSEGV))
  499.         {
  500.           /* Signal not for debugging purposes.  */
  501.           int printed = 0;
  502.  
  503.           if (stop_signal >= NSIG
  504.           || signal_print[stop_signal])
  505.         {
  506.           printed = 1;
  507.           terminal_ours_for_output ();
  508.          printf_filtered ("\nProgram received signal %d, %s\n",
  509.               stop_signal,
  510.               stop_signal < NSIG
  511.                ? sys_siglist[stop_signal]
  512.                : "(undocumented)");
  513.           fflush (stdout);
  514.         }
  515.           if (stop_signal >= NSIG
  516.           || signal_stop[stop_signal])
  517.         break;
  518.           /* If not going to stop, give terminal back
  519.          if we took it away.  */
  520.           else if (printed)
  521.         terminal_inferior ();
  522.         }
  523.  
  524.       /* Handle cases caused by hitting a breakpoint.  */
  525.  
  526.       if (!random_signal
  527.           && (stop_breakpoint || stop_step_resume_break))
  528.         {
  529.           /* Does a breakpoint want us to stop?  */
  530.           if (stop_breakpoint && stop_breakpoint != -1)
  531.         {
  532.           /* 0x1000000 is set in stop_breakpoint as returned by
  533.              breakpoint_status_p to indicate a silent breakpoint.  */
  534.           if (stop_breakpoint > 0 && stop_breakpoint & 0x1000000)
  535.             {
  536.               stop_breakpoint &= ~0x1000000;
  537.               stop_print_frame = 0;
  538.             }
  539.           break;
  540.         }
  541.           /* But if we have hit the step-resumption breakpoint,
  542.          remove it.  It has done its job getting us here.  */
  543.           if (stop_step_resume_break
  544.           && (step_frame == 0 || stop_frame == step_frame))
  545.         {
  546.           remove_step_breakpoint ();
  547.           step_resume_break_address = 0;
  548.           /* If were waiting for a trap, hitting the step_resume_break
  549.              doesn't count as getting it.  */
  550.           if (trap_expected)
  551.               another_trap = 1;
  552.         }
  553.           /* Otherwise, must remove breakpoints and single-step
  554.          to get us past the one we hit.  */
  555.           else
  556.         {
  557.           remove_breakpoints ();
  558.           remove_step_breakpoint ();
  559.           breakpoints_inserted = 0;
  560.           another_trap = 1;
  561.         }
  562.  
  563.           /* We come here if we hit a breakpoint but should not
  564.          stop for it.  Possibly we also were stepping
  565.          and should stop for that.  So fall through and
  566.          test for stepping.  But, if not stepping,
  567.          do not stop.  */
  568.         }
  569.  
  570.       /* If this is the breakpoint at the end of a stack dummy,
  571.          just stop silently.  */
  572.       if (stop_sp INNER_THAN stop_pc && stop_pc INNER_THAN stop_frame)
  573.         {
  574.           stop_print_frame = 0;
  575.           stop_stack_dummy = 1;
  576. #ifdef HP9K320
  577.           trap_expected_after_continue = 1;
  578. #endif
  579.           break;
  580.         }
  581.  
  582.       if (step_resume_break_address)
  583.         /* Having a step-resume breakpoint overrides anything
  584.            else having to do with stepping commands until
  585.            that breakpoint is reached.  */
  586.         ;
  587.       /* If stepping through a line, keep going if still within it.  */
  588.       else if (!random_signal
  589.            && step_range_end
  590.            && stop_pc >= step_range_start
  591.            && stop_pc < step_range_end)
  592.         {
  593. #if 0
  594.         /* When "next"ing through a function,
  595.            This causes an extra stop at the end.
  596.            Is there any reason for this?
  597.            It's confusing to the user.  */
  598.           /* Don't step through the return from a function
  599.          unless that is the first instruction stepped through.  */
  600.           if (ABOUT_TO_RETURN (stop_pc))
  601.         {
  602.           stop_step = 1;
  603.           break;
  604.         }
  605. #endif
  606.         }
  607.  
  608.       /* We stepped out of the stepping range.  See if that was due
  609.          to a subroutine call that we should proceed to the end of.  */
  610.       else if (!random_signal && step_range_end)
  611.         {
  612.           newfun = find_pc_function (stop_pc);
  613.           newmisc = -1;
  614.           if (newfun)
  615.         {
  616.           newfun_pc = BLOCK_START (SYMBOL_BLOCK_VALUE (newfun))
  617.             + FUNCTION_START_OFFSET;
  618.         }
  619.           else
  620.         {
  621.           newmisc = find_pc_misc_function (stop_pc);
  622.           if (newmisc >= 0)
  623.             newfun_pc = misc_function_vector[newmisc].address
  624.               + FUNCTION_START_OFFSET;
  625.           else newfun_pc = 0;
  626.         }
  627.           if (stop_pc == newfun_pc
  628.           && (step_over_calls > 0 || (step_over_calls && newfun == 0)))
  629.         {
  630.           /* A subroutine call has happened.  */
  631.           /* Set a special breakpoint after the return */
  632.           step_resume_break_address = SAVED_PC_AFTER_CALL (stop_frame);
  633.           step_resume_break_duplicate
  634.             = breakpoint_here_p (step_resume_break_address);
  635.           if (breakpoints_inserted)
  636.             insert_step_breakpoint ();
  637.         }
  638.           /* Subroutine call with source code we should not step over.
  639.          Do step to the first line of code in it.  */
  640.           else if (stop_pc == newfun_pc &&  step_over_calls)
  641.         {
  642.           SKIP_PROLOGUE (newfun_pc);
  643.           sal = find_pc_line (newfun_pc, 0);
  644.           /* Use the step_resume_break to step until
  645.              the end of the prologue, even if that involves jumps
  646.              (as it seems to on the vax under 4.2).  */
  647.           /* If the prologue ends in the middle of a source line,
  648.              continue to the end of that source line.
  649.              Otherwise, just go to end of prologue.  */
  650.           if (sal.end && sal.pc != newfun_pc)
  651.             newfun_pc = sal.end;
  652.  
  653.           if (newfun_pc == stop_pc)
  654.           {
  655.               /* We are already there: stop now.  */
  656.               stop_step = 1;
  657.               break;
  658.           }
  659.           else
  660.             /* Put the step-breakpoint there and go until there.  */
  661.             {
  662.               step_resume_break_address = newfun_pc;
  663.  
  664.               step_resume_break_duplicate
  665.             = breakpoint_here_p (step_resume_break_address);
  666.               if (breakpoints_inserted)
  667.             insert_step_breakpoint ();
  668.               /* Do not specify what the fp should be when we stop
  669.              since on some machines the prologue
  670.              is where the new fp value is established.  */
  671.               step_frame = 0;
  672.               /* And make sure stepping stops right away then.  */
  673.               step_range_end = step_range_start;
  674.             }
  675.         }
  676.           else
  677.         {
  678. #if 1
  679.           /* We get here only if step_over_calls is 0 and we
  680.              just stepped into a subroutine.  I presume
  681.              that step_over_calls is only 0 when we're
  682.              supposed to be stepping at the assembly
  683.              language level.*/
  684.           stop_step = 1;
  685.           break;
  686. #endif
  687.         }
  688.         }
  689.       /* No subroutince call; stop now.  */
  690.       else
  691.         {
  692. #if 0
  693.           stop_step = 1;
  694.           break;
  695. #endif
  696.         }
  697.     }
  698.  
  699.       /* Save the pc before execution, to compare with pc after stop.  */
  700.       prev_pc = read_pc ();
  701.  
  702.       /* If we did not do break;, it means we should keep
  703.      running the inferior and not return to debugger.  */
  704.  
  705.       /* If trap_expected is 2, it means continue once more
  706.      and insert breakpoints at the next trap.
  707.      If trap_expected is 1 and the signal was SIGSEGV, it means
  708.      the shell is doing some memory allocation--just resume it
  709.      with SIGSEGV.
  710.      Otherwise insert breakpoints now, and possibly single step.  */
  711.  
  712.       if (trap_expected > 1)
  713.     {
  714.       trap_expected--;
  715.       running_in_shell = 1;
  716.       resume (0, 0);
  717.     }
  718.       else if (running_in_shell && stop_signal == SIGSEGV)
  719.     {
  720.       resume (0, SIGSEGV);
  721.     }
  722.       else
  723.     {
  724.       /* Here, we are not awaiting another exec to get
  725.          the program we really want to debug.
  726.          Insert breakpoints now, unless we are trying
  727.          to one-proceed past a breakpoint.  */
  728.       running_in_shell = 0;
  729.       if (!breakpoints_inserted && !another_trap)
  730.         {
  731.           insert_step_breakpoint ();
  732.           breakpoints_failed = insert_breakpoints ();
  733.           if (breakpoints_failed)
  734.         break;
  735.           breakpoints_inserted = 1;
  736.         }
  737.  
  738.       trap_expected = another_trap;
  739.  
  740.       if (stop_signal == SIGTRAP)
  741.         stop_signal = 0;
  742.  
  743.       resume ((step_range_end && !step_resume_break_address)
  744.           || (trap_expected && !step_resume_break_address),
  745.           stop_signal);
  746.     }
  747.     }
  748. }
  749.  
  750. /* Here to return control to GDB when the inferior stops for real.
  751.    Print appropriate messages, remove breakpoints, give terminal our modes.
  752.  
  753.    RUNNING_IN_SHELL nonzero means the shell got a signal before
  754.    exec'ing the program we wanted to run.
  755.    STOP_PRINT_FRAME nonzero means print the executing frame
  756.    (pc, function, args, file, line number and line text).
  757.    BREAKPOINTS_FAILED nonzero means stop was due to error
  758.    attempting to insert breakpoints.  */
  759.  
  760. static void
  761. normal_stop ()
  762. {
  763.  
  764.   if (breakpoints_failed)
  765.     {
  766.       terminal_ours_for_output ();
  767.       print_sys_errmsg ("ptrace", breakpoints_failed);
  768.      printf_filtered ("Stopped; cannot insert breakpoints.\n\
  769. The same program may be running in another process.\n");
  770.     }
  771.  
  772.   if (inferior_pid)
  773.     remove_step_breakpoint ();
  774.  
  775.   if (inferior_pid && breakpoints_inserted)
  776.     if (remove_breakpoints ())
  777.       {
  778.     terminal_ours_for_output ();
  779. printf_filtered ("Cannot remove breakpoints because program is no longer writable.\n\
  780. It must be running in another process.\n\
  781. Further execution is probably impossible.\n");
  782.       }
  783.  
  784.   breakpoints_inserted = 0;
  785.  
  786.   /* Delete the breakpoint we stopped at, if it wants to be deleted.
  787.      Delete any breakpoint that is to be deleted at the next stop.  */
  788.  
  789.   breakpoint_auto_delete (stop_breakpoint);
  790.  
  791.   /* If an auto-display called a function and that got a signal,
  792.      delete that auto-display to avoid an infinite recursion.  */
  793.  
  794.   delete_current_display ();
  795.  
  796.   if (step_multi && stop_step)
  797.     return;
  798.  
  799.   terminal_ours ();
  800.  
  801.   if (running_in_shell)
  802.     {
  803.       if (stop_signal == SIGSEGV)
  804. printf_filtered ("\
  805. You have just encountered a bug in \"sh\".  GDB starts your program\n\
  806. by running \"sh\" with a command to exec your program.\n\
  807. This is so that \"sh\" will process wildcards and I/O redirection.\n\
  808. This time, \"sh\" crashed.\n\
  809. \n\
  810. One known bug in \"sh\" bites when the environment takes up a lot of space.\n\
  811. Try \"info env\" to see the environment; then use \"unset-env\" to kill\n\
  812. some variables whose values are large; then do \"run\" again.\n\
  813. \n\
  814. If that works, you might want to put those \"unset-env\" commands\n\
  815. into a \".gdbinit\" file in this directory so they will happen every time.\n");
  816.       /* Don't confuse user with his program's symbols on sh's data.  */
  817.       stop_print_frame = 0;
  818.     }
  819.  
  820.   if (inferior_pid == 0)
  821.     return;
  822.  
  823.   /* Select innermost stack frame except on return from a stack dummy routine,
  824.      or if the program has exited.  */
  825.   if (!stop_stack_dummy)
  826.     {
  827. #if 1
  828.       select_frame (stop_frame, 0);
  829. #else      
  830.       select_frame (get_current_frame(), 0);
  831. #endif
  832.  
  833.       if (stop_print_frame)
  834.     {
  835.       if (stop_breakpoint > 0)
  836.        printf_filtered ("\nBpt %d, ", stop_breakpoint);
  837.       print_sel_frame (stop_step
  838.                && step_frame == stop_frame
  839.                && step_start_function == find_pc_function (stop_pc));
  840.       /* Display the auto-display expressions.  */
  841.       do_displays ();
  842.     }
  843.     }
  844.  
  845.   /* Save the function value return registers
  846.      We might be about to restore their previous contents.  */
  847. /*  if (proceed_to_finish) */
  848.     read_register_bytes (0, stop_registers, REGISTER_BYTES);
  849.  
  850.   if (stop_stack_dummy)
  851.     {
  852.       /* Pop the empty frame that contains the stack dummy.  */
  853.       POP_FRAME;
  854. #if 1
  855.       select_frame (read_register (FP_REGNUM), 0);
  856. #else      
  857.       select_frame (get_current_frame(), 0);
  858. #endif
  859.     }
  860. }
  861.  
  862. static void
  863. insert_step_breakpoint ()
  864. {
  865.   if (step_resume_break_address && !step_resume_break_duplicate)
  866.     {
  867.       read_memory (step_resume_break_address,
  868.            step_resume_break_shadow, sizeof break_insn);
  869.       write_memory (step_resume_break_address,
  870.             break_insn, sizeof break_insn);
  871.     }
  872. }
  873.  
  874. static void
  875. remove_step_breakpoint ()
  876. {
  877.   if (step_resume_break_address && !step_resume_break_duplicate)
  878.     write_memory (step_resume_break_address, step_resume_break_shadow,
  879.           sizeof break_insn);
  880. }
  881.  
  882. /* Specify how various signals in the inferior should be handled.  */
  883.  
  884. static void
  885. handle_command (args, from_tty)
  886.      char *args;
  887.      int from_tty;
  888. {
  889.   register char *p = args;
  890.   int signum;
  891.   register int digits, wordlen;
  892.  
  893.   if (!args)
  894.     error_no_arg ("signal to handle");
  895.  
  896.   while (*p)
  897.     {
  898.       /* Find the end of the next word in the args.  */
  899.       for (wordlen = 0; p[wordlen] && p[wordlen] != ' ' && p[wordlen] != '\t';
  900.        wordlen++);
  901.       for (digits = 0; p[digits] >= '0' && p[digits] <= '9'; digits++);
  902.  
  903.       /* If it is all digits, it is signal number to operate on.  */
  904.       if (digits == wordlen)
  905.     {
  906.       signum = atoi (p);
  907.       if (signum == SIGTRAP || signum == SIGINT)
  908.         {
  909.           if (!query ("Signal %d is used by the debugger.\nAre you sure you want to change it? ", signum))
  910.         error ("Not confirmed.");
  911.         }
  912.     }
  913.       else if (signum == 0)
  914.     error ("First argument is not a signal number.");
  915.  
  916.       /* Else, if already got a signal number, look for flag words
  917.      saying what to do for it.  */
  918.       else if (!strncmp (p, "stop", wordlen))
  919.     {
  920.       signal_stop[signum] = 1;
  921.       signal_print[signum] = 1;
  922.     }
  923.       else if (wordlen >= 2 && !strncmp (p, "print", wordlen))
  924.     signal_print[signum] = 1;
  925.       else if (wordlen >= 2 && !strncmp (p, "pass", wordlen))
  926.     signal_program[signum] = 1;
  927.       else if (!strncmp (p, "ignore", wordlen))
  928.     signal_program[signum] = 0;
  929.       else if (wordlen >= 3 && !strncmp (p, "nostop", wordlen))
  930.     signal_stop[signum] = 0;
  931.       else if (wordlen >= 4 && !strncmp (p, "noprint", wordlen))
  932.     {
  933.       signal_print[signum] = 0;
  934.       signal_stop[signum] = 0;
  935.     }
  936.       else if (wordlen >= 4 && !strncmp (p, "nopass", wordlen))
  937.     signal_program[signum] = 0;
  938.       else if (wordlen >= 3 && !strncmp (p, "noignore", wordlen))
  939.     signal_program[signum] = 1;
  940.       /* Not a number and not a recognized flag word => complain.  */
  941.       else
  942.     {
  943.       p[wordlen] = 0;
  944.       error ("Unrecognized flag word: \"%s\".", p);
  945.     }
  946.  
  947.       /* Find start of next word.  */
  948.       p += wordlen;
  949.       while (*p == ' ' || *p == '\t') p++;
  950.     }
  951.  
  952.   if (from_tty)
  953.     {
  954.       /* Show the results.  */
  955.      printf_filtered ("Number\tStop\tPrint\tPass to program\tDescription\n");
  956.      printf_filtered ("%d\t", signum);
  957.      printf_filtered ("%s\t", signal_stop[signum] ? "Yes" : "No");
  958.      printf_filtered ("%s\t", signal_print[signum] ? "Yes" : "No");
  959.      printf_filtered ("%s\t\t", signal_program[signum] ? "Yes" : "No");
  960.      printf_filtered ("%s\n", sys_siglist[signum]);
  961.     }
  962. }
  963.  
  964. /* Print current contents of the tables set by the handle command.  */
  965.  
  966. static void
  967. signals_info (signum_exp)
  968.      char *signum_exp;
  969. {
  970.   register int i;
  971.  printf_filtered ("Number\tStop\tPrint\tPass to program\tDescription\n");
  972.  
  973.   if (signum_exp)
  974.     {
  975.       i = parse_and_eval_address (signum_exp);
  976.      printf_filtered ("%d\t", i);
  977.      printf_filtered ("%s\t", signal_stop[i] ? "Yes" : "No");
  978.      printf_filtered ("%s\t", signal_print[i] ? "Yes" : "No");
  979.      printf_filtered ("%s\t\t", signal_program[i] ? "Yes" : "No");
  980.      printf_filtered ("%s\n", sys_siglist[i]);
  981.       return;
  982.     }
  983.  
  984.  printf_filtered ("\n");
  985.   for (i = 0; i < NSIG; i++)
  986.     {
  987.       QUIT;
  988.       if (i > 0 && i % 16 == 0)
  989.     {
  990.      printf_filtered ("[Type Return to see more]");
  991.       fflush (stdout);
  992.       read_line ();
  993.     }
  994.      printf_filtered ("%d\t", i);
  995.      printf_filtered ("%s\t", signal_stop[i] ? "Yes" : "No");
  996.      printf_filtered ("%s\t", signal_print[i] ? "Yes" : "No");
  997.      printf_filtered ("%s\t\t", signal_program[i] ? "Yes" : "No");
  998.      printf_filtered ("%s\n", sys_siglist[i]);
  999.     }
  1000.  
  1001.  printf_filtered ("\nUse the \"handle\" command to change these tables.\n");
  1002. }
  1003.  
  1004. /* Save all of the information associated with the inferior<==>gdb
  1005.    connection.  INF_STATUS is a pointer to a "struct inferior_status"
  1006.    (defined in inferior.h).  */
  1007.  
  1008. void
  1009. save_inferior_status (inf_status, restore_stack_info)
  1010.      struct inferior_status *inf_status;
  1011.      int restore_stack_info;
  1012. {
  1013.   struct command_line *get_breakpoint_commands ();
  1014.  
  1015.   inf_status->pc_changed = pc_changed;
  1016.   inf_status->stop_signal = stop_signal;
  1017.   inf_status->stop_pc = stop_pc;
  1018.   inf_status->stop_frame = stop_frame;
  1019.   inf_status->stop_step = stop_step;
  1020.   inf_status->stop_stack_dummy = stop_stack_dummy;
  1021.   inf_status->trap_expected = trap_expected;
  1022.   inf_status->step_range_start = step_range_start;
  1023.   inf_status->step_range_end = step_range_end;
  1024.   inf_status->step_frame = step_frame;
  1025.   inf_status->step_over_calls = step_over_calls;
  1026.   inf_status->step_resume_break_address = step_resume_break_address;
  1027.   inf_status->stop_after_trap = stop_after_trap;
  1028.  
  1029.   inf_status->stop_breakpoint = stop_breakpoint;
  1030.   inf_status->breakpoint_commands = get_breakpoint_commands();
  1031.  
  1032.   inf_status->restore_stack_info = restore_stack_info;
  1033.   inf_status->proceed_to_finish = proceed_to_finish;
  1034.   
  1035.   bcopy (stop_registers, inf_status->stop_registers, REGISTER_BYTES);
  1036.   
  1037.   record_selected_frame (&(inf_status->selected_frame),
  1038.              &(inf_status->selected_level));
  1039.   return;
  1040. }
  1041.  
  1042. void
  1043. restore_inferior_status (inf_status)
  1044.      struct inferior_status *inf_status;
  1045. {
  1046.   FRAME fid;
  1047.   int level = inf_status->selected_level;
  1048.  
  1049.   pc_changed = inf_status->pc_changed;
  1050.   stop_signal = inf_status->stop_signal;
  1051.   stop_pc = inf_status->stop_pc;
  1052.   stop_frame = inf_status->stop_frame;
  1053.   stop_step = inf_status->stop_step;
  1054.   stop_stack_dummy = inf_status->stop_stack_dummy;
  1055.   trap_expected = inf_status->trap_expected;
  1056.   step_range_start = inf_status->step_range_start;
  1057.   step_range_end = inf_status->step_range_end;
  1058.   step_frame = inf_status->step_frame;
  1059.   step_over_calls = inf_status->step_over_calls;
  1060.   step_resume_break_address = inf_status->step_resume_break_address;
  1061.   stop_after_trap = inf_status->stop_after_trap;
  1062.  
  1063.   inf_status->stop_breakpoint = stop_breakpoint;
  1064.   set_breakpoint_commands (inf_status->breakpoint_commands);
  1065.  
  1066.   proceed_to_finish = inf_status->proceed_to_finish;
  1067.  
  1068.   bcopy (inf_status->stop_registers, stop_registers, REGISTER_BYTES);
  1069.  
  1070. #if 0
  1071.   select_frame (inf_status->selected_frame, inf_status->selected_level);
  1072.   return;
  1073. #endif
  1074.  
  1075.   /* The inferior can be gone if the user types "print exit(0)"
  1076.      (and perhaps other times).  */
  1077.   if ( 1 && inf_status->restore_stack_info)
  1078.     {
  1079.       fid = find_relative_frame (get_current_frame (),
  1080.                  &level);
  1081.  
  1082.       /* If inf_status->selected_frame_address is NULL, there was no
  1083.      previously selected frame.  */
  1084.       if (fid == 0 ||
  1085.       fid != inf_status->selected_frame ||
  1086.       level != 0)
  1087.     {
  1088. #if 0
  1089.       /* I'm not sure this error message is a good idea.  I have
  1090.          only seen it occur after "Can't continue previously
  1091.          requested operation" (we get called from do_cleanups), in
  1092.          which case it just adds insult to injury (one confusing
  1093.          error message after another.  Besides which, does the
  1094.          user really care if we can't restore the previously
  1095.          selected frame?  */
  1096.       fprintf_filtered (stderr, "Unable to restore previously selected frame.\n");
  1097. #endif
  1098.       select_frame (get_current_frame (), 0);
  1099.       return;
  1100.     }
  1101.       
  1102.       select_frame (fid, inf_status->selected_level);
  1103.     }
  1104. }
  1105.  
  1106.  
  1107. void
  1108. initialize_st_infru ()
  1109. {
  1110.   register int i;
  1111.  
  1112.   add_info ("signals", signals_info,
  1113.         "What debugger does when program gets various signals.\n\
  1114. Specify a signal number as argument to print info on that signal only.");
  1115.  
  1116.   add_com ("handle", class_run, handle_command,
  1117.        "Specify how to handle a signal.\n\
  1118. Args are signal number followed by flags.\n\
  1119. Flags allowed are \"stop\", \"print\", \"pass\",\n\
  1120.  \"nostop\", \"noprint\" or \"nopass\".\n\
  1121. Print means print a message if this signal happens.\n\
  1122. Stop means reenter debugger if this signal happens (implies print).\n\
  1123. Pass means let program see this signal; otherwise program doesn't know.\n\
  1124. Pass and Stop may be combined.");
  1125.  
  1126.   for (i = 0; i < NSIG; i++)
  1127.     {
  1128.       signal_stop[i] = 1;
  1129.       signal_print[i] = 1;
  1130.       signal_program[i] = 1;
  1131.     }
  1132.  
  1133.   /* Signals caused by debugger's own actions
  1134.      should not be given to the program afterwards.  */
  1135.   signal_program[SIGTRAP] = 0;
  1136.   signal_program[SIGINT] = 0;
  1137.  
  1138.   /* Signals that are not errors should not normally enter the debugger.  */
  1139. #ifdef SIGALRM
  1140.   signal_stop[SIGALRM] = 0;
  1141.   signal_print[SIGALRM] = 0;
  1142. #endif /* SIGALRM */
  1143. #ifdef SIGVTALRM
  1144.   signal_stop[SIGVTALRM] = 0;
  1145.   signal_print[SIGVTALRM] = 0;
  1146. #endif /* SIGVTALRM */
  1147. #ifdef SIGPROF
  1148.   signal_stop[SIGPROF] = 0;
  1149.   signal_print[SIGPROF] = 0;
  1150. #endif /* SIGPROF */
  1151. #ifdef SIGCHLD
  1152.   signal_stop[SIGCHLD] = 0;
  1153.   signal_print[SIGCHLD] = 0;
  1154. #endif /* SIGCHLD */
  1155. #ifdef SIGCLD
  1156.   signal_stop[SIGCLD] = 0;
  1157.   signal_print[SIGCLD] = 0;
  1158. #endif /* SIGCLD */
  1159. #ifdef SIGIO
  1160.   signal_stop[SIGIO] = 0;
  1161.   signal_print[SIGIO] = 0;
  1162. #endif /* SIGIO */
  1163. #ifdef SIGURG
  1164.   signal_stop[SIGURG] = 0;
  1165.   signal_print[SIGURG] = 0;
  1166. #endif /* SIGURG */
  1167. }
  1168.